var fs = require('fs')
const nodeXlsx = require('node-xlsx')
const axios = require('axios')
const path1 = `${__dirname}/data`;

axios.get('http://127.0.0.1:8881/weibo/search').then(res => {
   let tableData = res.data.data
      if (fs.existsSync(`${__dirname}/data.xls`)) {
    fs.unlinkSync(`${__dirname}/data.xls`);
  }
  var buffer = null;
  var excelData = [];
  var addInfo = {};
  //名称
  addInfo.name = "微博评论";
  //数据数组
  addInfo.data = [["时间", "微博内容", "评论数", "热门评论"]];
  tableData.forEach(item => {
    addInfo.data.push([
        item.pub_date,
        item.content,
        item.comment_count,
        item.comment[0],
      ])
  })
  //添加数据
  excelData.push(addInfo);
  // 写xlsx
  buffer = nodeXlsx.build(excelData);
  //写入数据
  fs.writeFile(`${__dirname}/data.xls`, buffer, function (err) {
    if (err) {
      throw err;
    }
    console.log(excelData[0].data.length, "Write to xls has finished");
  });
})
